package com.logos.common.animator;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;

import com.logos.tool.string.StringUtil;

public class GenericAnimator {

    public static class Builder {
        View mView;
        String mPropertyName;
        float mFrom = 0;
        float mTo = 0;
        int mDuration = 300;
        Interpolator mInterpolator = new LinearInterpolator();

        OnAnimationEndListener mEndListener;
        OnAnimationStartListener mStartListener;
        OnAnimationCancelListener mCancelListener;
        OnAnimationRepeatListener mRepeatListener;

        public Builder view(View view) {
            mView = view;
            return this;
        }

        public Builder propertyName(String propertyName) {
            mPropertyName = propertyName;
            return this;
        }

        public Builder from(float from) {
            mFrom = from;
            return this;
        }

        public Builder to(float to) {
            mTo = to;
            return this;
        }

        public Builder duration(int duration) {
            mDuration = duration;
            return this;
        }

        public Builder interpolator(Interpolator interpolator) {
            mInterpolator = interpolator;
            return this;
        }


        public Builder startListener(OnAnimationStartListener startListener) {
            mStartListener = startListener;
            return this;
        }

        public Builder cancelListener(OnAnimationCancelListener cancelListener) {
            mCancelListener = cancelListener;
            return this;
        }

        public Builder endListener(OnAnimationEndListener endListener) {
            mEndListener = endListener;
            return this;
        }

        public Builder repeatListener(OnAnimationRepeatListener repeatListener) {
            mRepeatListener = repeatListener;
            return this;
        }

        public GenericAnimator build() {

            if (mView == null) {
                throw new IllegalArgumentException("view is null");
            }

            if (StringUtil.isEmpty(mPropertyName)) {
                throw new IllegalArgumentException("propertyName is null");
            }
            return new GenericAnimator();
        }
    }

    public Builder newBuilder() {
        return new Builder();
    }

    public void start(final Builder builder) {
        final View view = builder.mView;

        ObjectAnimator valueAnimator = ObjectAnimator.ofFloat(view, builder.mPropertyName, builder.mFrom, builder.mTo);
        valueAnimator.setDuration(builder.mDuration);
        valueAnimator.setInterpolator(builder.mInterpolator);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                view.requestLayout();
            }
        });
        valueAnimator.addListener(new android.animation.Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

                if (builder.mStartListener != null) {
                    builder.mStartListener.onAnimationStart(animation);
                }
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (builder.mEndListener != null) {
                    builder.mEndListener.onAnimationEnd(animation);
                }

            }

            @Override
            public void onAnimationCancel(Animator animation) {
                if (builder.mCancelListener != null) {
                    builder.mCancelListener.onAnimationCancel(animation);
                }
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                if (builder.mRepeatListener != null) {
                    builder.mRepeatListener.onAnimationRepeat(animation);
                }
            }
        });
        valueAnimator.start();
    }

    public interface OnAnimationEndListener {
        void onAnimationEnd(Animator animation);
    }

    public interface OnAnimationStartListener {
        void onAnimationStart(Animator animation);
    }

    public interface OnAnimationCancelListener {
        void onAnimationCancel(Animator animation);
    }

    public interface OnAnimationRepeatListener {
        void onAnimationRepeat(Animator animation);
    }
}
